package com.jiesen.base.modules.company.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiesen.base.modules.company.model.JsPublicUsersGroupby;
import com.jiesen.base.modules.company.service.JsPublicUsersGroupbyService;
import com.jiesen.base.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/jsPublicUsersGroupby")
public class JsPublicUsersGroupbyController {

    @Autowired
    private JsPublicUsersGroupbyService jsPublicUsersGroupbyService;

    //增
    @PostMapping("/create")
    public R create(@RequestBody JsPublicUsersGroupby jsPublicUsersGroupby){
        if(jsPublicUsersGroupby.getName()==null){
            return R.error("名称为空");
        }
        jsPublicUsersGroupby.setLock(1);
        jsPublicUsersGroupby.setCts(new Date());
        boolean save = jsPublicUsersGroupbyService.save(jsPublicUsersGroupby);
        if(!save){
            return R.error("添加失败");
        }
        return R.ok();
    }

    //删
    @GetMapping("/delete")
    public R delete(@RequestParam("id")Integer id){
        boolean b = jsPublicUsersGroupbyService.removeById(id);
        if(!b){
            return R.error();
        }
        return R.ok();
    }

    //改
    @PostMapping("/edit")
    public R edit(@RequestBody JsPublicUsersGroupby jsPublicUsersGroupby){
        boolean b = jsPublicUsersGroupbyService.updateById(jsPublicUsersGroupby);
        if(!b){
            return R.error();
        }
        return R.ok();
    }

    //查询单个
    @GetMapping("/view")
    public R view(@RequestParam("id")Integer id){
        JsPublicUsersGroupby jsPublicUsersGroupby = jsPublicUsersGroupbyService.getById(id);
        return R.ok().put("data", jsPublicUsersGroupby);
    }

    //根据条件查询批次
    @PostMapping("/view")
    public R view(@RequestBody JsPublicUsersGroupby jsPublicUsersGroupby){
        return jsPublicUsersGroupbyService.page(jsPublicUsersGroupby);
    }

    //根据分校信息查询批次
    @GetMapping("/viewBranch")
    public R viewBranch(@RequestParam("branchId")Integer id){
        QueryWrapper<JsPublicUsersGroupby> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("branch_id", id).eq("`lock`",1);
        List<JsPublicUsersGroupby> list = jsPublicUsersGroupbyService.list(queryWrapper);
        return R.ok().put("data",list);
    }

}
